package com.four.seckill.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.four.common.utils.PageUtils;
import com.four.common.utils.R;
import com.four.common.vo.CommodityTo;
import com.four.seckill.entity.CommodityEntity;
import com.four.seckill.service.CommodityService;
import com.four.seckill.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: hxw
 * @Date: 2022/01/06/11:31
 * @Description
 */

@RestController
@RequestMapping("seckill/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    /**
     * 秒杀设置列表 状态为0的
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("comList")
    public R list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                  @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        PageUtils list = commodityService.getList(page, limit);
        return R.ok().put("data", list);
    }

    /**
     * 商品添加成秒杀商品
     *
     * @param commodityEntity
     * @return
     */
    @PostMapping("saveUpdCommodity")
    public R saveUpdCommodity(@RequestBody List<CommodityEntity> commodityEntity) {

        return commodityService.saveUpdCommodity(commodityEntity);
    }

    /**
     * 添加秒杀商品前的删除缓存
     * @param id
     * @return
     */
    @DeleteMapping("deleteCommodityRedis/{id}")
    public R deleteCommodityRedis(@PathVariable("id") Integer id) {

        return commodityService.deleteCommodityRedis(id);
    }


    /**
     * 商品根据分类id分页模糊查 状态为1的
     * @param page
     * @param size
     * @param vo
     * @return
     */
    @PostMapping("/commodityList/{catId}")
    public R commodityList(@PathVariable("catId")Integer catId,
                           @RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "size",defaultValue = "3")Integer size,
                           CommodityVo vo
    ){
        IPage<CommodityEntity> commodityList=commodityService.commodityList(page,size,vo,catId);
        return R.ok().put("commodityList",commodityList);
    }

    /**
     * 查询是否符合秒杀的商品
     * @param comId
     * @return
     */
    @GetMapping("/commoditySeckill/{comId}")
    public R commoditySeckill(@PathVariable("comId") Integer comId) {

        return commodityService.commoditySeckill(comId);
    }


    /**
     * 添加商品
     * @param commodity
     * @return
     */
    @PostMapping("/addCommdity")
    public R addCommdity(@RequestBody CommodityEntity commodity){
        commodityService.addCommdity(commodity);
        return R.ok("添加成功");
    }

    /**
     * 根据id删除商品
     * @param comId
     * @return
     */
    @DeleteMapping("/deleteById/{comId}")
    public R deleteById(@PathVariable("comId")Integer comId){
        commodityService.deleteById(comId);
        return R.ok("删除成功");
    }

    /**
     * 根据id查询商品
     * @param comId
     * @return
     */
    @GetMapping("getCommodityById/{comId}")
    public R getCommodityById(@PathVariable("comId")Integer comId){
        CommodityEntity commodity=commodityService.getCommodityById(comId);
        return R.ok().put("commodity",commodity);
    }

    /**
     * 根据id修改商品
     * @param comId
     * @param commodity
     * @return
     */
    @PutMapping("/updateCommodityById/{comId}")
    public R updateCommodityById(@PathVariable("comId")Integer comId,@RequestBody CommodityEntity commodity){
        commodity.setComId(comId);
        commodityService.updateCommodityById(commodity);
        return R.ok("修改成功");
    }

    /**
     * 启动商品秒杀
     * @return
     */
    @GetMapping("/seckillList")
    public R seckillList() {

        return commodityService.seckillList();
    }

    /**
     * 获取一个token值以防止幂等，或者说防止用户重复提交
     * @return
     */
    @GetMapping("/tokenKey/{comId}/{userToken}")
    public String tokenKey(@PathVariable("comId") Integer comId,
                           @PathVariable("userToken") String userToken) {
        return commodityService.tokenKey(comId,userToken);
    }

    /**
     * 秒杀商品
     * @param comId
     * @param userToken
     * @return
     */
    @GetMapping("seckillStatus/{comId}/{userToken}")
    public R seckillStatus(@PathVariable("comId") Integer comId,
                           @PathVariable("userToken") String userToken,
                           @PathVariable("token") String token) {

        return commodityService.seckillStatus(comId, userToken,token);
    }

    /**
     * feign调用根据编号查询商品的详情
     * @param commId
     * @return
     */
    @GetMapping("/getByCommId")
    public CommodityTo getByCommId(@RequestParam("commId") Integer commId) {

        return commodityService.getByCommId(commId);
    }

    /**
     * 修改状态
     * @param byCommId
     * @return
     */
    @PostMapping("/updateByStatus")
    public R updateByStatus(@RequestBody CommodityTo byCommId) {

        return commodityService.updateByStatus(byCommId);
    }
}
